package clientUtilGUI;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import clientMain.Login;
import clientMain.Main;
import clientView.Contract_View;
import clientView.Dip_View;
import clientView.EmT_View;
import clientView.Pos_View;
import clientView.Province_View;
import clientView.Spe_View;

public class Tab_Utility extends JToolBar implements ActionListener{
	
	private JButton btnProvince;
	private JButton btnEmT;
	private JButton btnSpec;
	private JButton btnPosition;
	private JButton btnContract;
	private JButton btnDiplomas;

	public Tab_Utility(){
		setFloatable(false);
		setOrientation(JToolBar.HORIZONTAL);
		setMargin(new Insets(0, 6, 0, 2));
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Province") ||
					Login.listAuthorization.get(i).getModuleName().equals("District")){
				btnProvince = new JButton(UtilityGUI.subMenuProvince,new ImageIcon("src/image/maps.png"));
				btnProvince.setVerticalTextPosition(JButton.BOTTOM);
				btnProvince.setHorizontalTextPosition(JButton.CENTER);
				btnProvince.addActionListener(this);
				add(btnProvince);
				break;
			}
		}
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("EmployeeType")){
				btnEmT = new JButton(UtilityGUI.subMenuEmT,new ImageIcon("src/image/job_icon.png"));
				btnEmT.setVerticalTextPosition(JButton.BOTTOM);
				btnEmT.setHorizontalTextPosition(JButton.CENTER);
				btnEmT.addActionListener(this);
				add(btnEmT);
				break;
			}
		}
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Position")){
				btnPosition = new JButton(UtilityGUI.subMenuPosition,new ImageIcon("src/image/testimonials.png"));
				btnPosition.setVerticalTextPosition(JButton.BOTTOM);
				btnPosition.setHorizontalTextPosition(JButton.CENTER);
				btnPosition.addActionListener(this);
				add(btnPosition);
				break;
			}
		}
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Contract")){
				btnContract = new JButton(UtilityGUI.subMenuContract,new ImageIcon("src/image/sign.png"));
				btnContract.setVerticalTextPosition(JButton.BOTTOM);
				btnContract.setHorizontalTextPosition(JButton.CENTER);
				btnContract.addActionListener(this);
				add(btnContract);
				break;
			}
		}
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Diplomas")){
				btnDiplomas = new JButton(UtilityGUI.subMenuDiplomas,new ImageIcon("src/image/graduated.png"));
				btnDiplomas.setVerticalTextPosition(JButton.BOTTOM);
				btnDiplomas.setHorizontalTextPosition(JButton.CENTER);
				btnDiplomas.addActionListener(this);
				add(btnDiplomas);
				break;
			}
		}

		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Specialized")){
				btnSpec = new JButton(UtilityGUI.subMenuSpecialized,new ImageIcon("src/image/contact-list.png"));
				btnSpec.setVerticalTextPosition(JButton.BOTTOM);
				btnSpec.setHorizontalTextPosition(JButton.CENTER);
				btnSpec.addActionListener(this);
				add(btnSpec);
				break;
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()== btnEmT){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmT);
			if(index == -1){
				Main.jTabbedPane.addTab(new EmT_View(), UtilityGUI.tabEmT);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		
     	if(e.getSource()== btnContract){
     		int index = Main.jTabbedPane.positionTab(UtilityGUI.tabContract);
    		if(index == -1){
    			Main.jTabbedPane.addTab(new Contract_View(), UtilityGUI.tabContract);
    			Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
    		}else{
    			Main.jTabbedPane.setSelectedIndex(index);
    			return;
    		}
		}
     	
     	if(e.getSource()== btnProvince){
     		int index = Main.jTabbedPane.positionTab(UtilityGUI.tabProvince);
    		if(index == -1){
    			Main.jTabbedPane.addTab(new Province_View(), UtilityGUI.tabProvince);
    			Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
    		}else{
    			Main.jTabbedPane.setSelectedIndex(index);
    			return;
    		}
		}
     	
     	if(e.getSource()== btnPosition){
     		int index = Main.jTabbedPane.positionTab(UtilityGUI.tabPosition);
    		if(index == -1){
    			Main.jTabbedPane.addTab(new Pos_View(), UtilityGUI.tabPosition);
    			Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
    		}else{
    			Main.jTabbedPane.setSelectedIndex(index);
    			return;
    		}
		}
     	
    	if(e.getSource()== btnDiplomas){
    		int index = Main.jTabbedPane.positionTab(UtilityGUI.tabDiplomas);
    		if(index == -1){
    			Main.jTabbedPane.addTab(new Dip_View(), UtilityGUI.tabDiplomas);
    			Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
    		}else{
    			Main.jTabbedPane.setSelectedIndex(index);
    			return;
    		}
		}
    	
      	if(e.getSource()== btnSpec){
      		int index = Main.jTabbedPane.positionTab(UtilityGUI.tabSpecialized);
    		if(index == -1){
    			Main.jTabbedPane.addTab(new Spe_View(), UtilityGUI.tabSpecialized);
    			Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
    		}else{
    			Main.jTabbedPane.setSelectedIndex(index);
    			return;
    		}
		}
	}
	
}

